package com.xingkeduo.controller.cms.marketing;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.MarketingBaseController;
import com.xingkeduo.model.param.MarketingParam;
import com.xingkeduo.model.param.MarketingParam.MarketingCouponStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingReceiveTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingServiceTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.service.cms.marketing.MarketingTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: ManagementCouponBagController
 * @Description: 市场营销管理-券包处理相关
 * @author lixin
 * @date Apr 18, 2019 9:11:07 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/couponBag")
public class CouponBagTemplateCmsController extends MarketingBaseController {
	Logger logger = LoggerFactory.getLogger(CouponBagTemplateCmsController.class);

	@Autowired
	@Qualifier(value = "marketingCouponBagTemplateService")
	MarketingTemplateService marketingCouponBagTemplateService;

	@RequestMapping("/addCouponBagTemplate")
	public ResponseMessage addCouponBagTemplate(@RequestBody MarketingParam couponBagTemplate) {
		couponBagTemplate.setMarketingType(MarketingTypeEnum.COUPON_BAG.getCode());
		couponBagTemplate.setStatus(MarketingStatusEnum.DOWN.getCode());
		try {
			this.initParams(couponBagTemplate);
			return marketingCouponBagTemplateService.saveOrUpdateCouponByTemplate(couponBagTemplate);
		} catch (Exception e) {
			logger.error("【添加优惠包异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getCouponBagTemplateDetail")
	public ResponseMessage getCouponBagTemplateDetail(Long couponBagTemplateId) {
		if (couponBagTemplateId == null) {
			return ResponseMessage.error("优惠包id不合法", 500);
		}
		return marketingCouponBagTemplateService.queryCouponById(couponBagTemplateId);
	}

	@RequestMapping("/updateCouponBagTemplate")
	public ResponseMessage updateCouponBagTemplate(@RequestBody MarketingParam couponBagTemplate) {
		couponBagTemplate.setMarketingType(MarketingTypeEnum.COUPON_BAG.getCode());
		try {
			this.initParams(couponBagTemplate);
			return marketingCouponBagTemplateService.saveOrUpdateCouponByTemplate(couponBagTemplate);
		} catch (Exception e) {
			logger.error("【修改优惠包异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/auditCouponBagTemplate")
	public ResponseMessage auditCouponBagTemplate(Long couponBagTemplateId, String status) {
		if (couponBagTemplateId == null) {
			return ResponseMessage.error("券包id不合法", 500);
		}
		if (MarketingStatusEnum.parseStatus(Integer.parseInt(status)).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券包状态不合法", 500);
		}
		MarketingParam couponBagTemplate = new MarketingParam();
		couponBagTemplate.setMarketingType(MarketingTypeEnum.COUPON_BAG.getCode());
		couponBagTemplate.setId(couponBagTemplateId);
		couponBagTemplate.setStatus(Integer.parseInt(status));
		this.initUserInParams(couponBagTemplate);
		return marketingCouponBagTemplateService.updateCouponStatusById(couponBagTemplate);
	}

	@RequestMapping("/getCouponBagTemplateList")
	public ResponseMessage getCouponBagTemplateList(MarketingParam couponBagTemplateExample) {
		couponBagTemplateExample.setMarketingType(MarketingTypeEnum.COUPON_BAG.getCode());
		// 券类型
		if (StringUtils.isNotBlank(couponBagTemplateExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(couponBagTemplateExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券包类型不合法", 500);
		}
		// 券状态
		if (StringUtils.isNotBlank(couponBagTemplateExample.getConponStatus()) && MarketingCouponStatusEnum
				.parse(couponBagTemplateExample.getConponStatus()).equals(MarketingCouponStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券包状态不合法", 500);
		}
		// 领取范围
		if (couponBagTemplateExample.getReceiveType() != null && MarketingReceiveTypeEnum
				.parseStatus(couponBagTemplateExample.getReceiveType()).equals(MarketingReceiveTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("券包领取范围不合法", 500);
		}
		// 上下架
		if (couponBagTemplateExample.getStatus() != null && MarketingStatusEnum
				.parseStatus(couponBagTemplateExample.getStatus()).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("券包上下架状态不合法", 500);
		}
		// 是否开启兑换码
		if (couponBagTemplateExample.getIsOpenConvert() != null
				&& Constant.STATUS_NO != couponBagTemplateExample.getIsOpenConvert()
				&& Constant.STATUS_YES != couponBagTemplateExample.getIsOpenConvert()) {
			return ResponseMessage.error("券包开启兑换码状态不合法", 500);
		}

		return marketingCouponBagTemplateService.queryCouponByTemplate(couponBagTemplateExample);
	}

}
